package advance;

public class RingList {
    public static boolean isRing(Node head){
        if(head==null) return false;
        //定义快慢指针
        Node slow=head; //慢指针
        Node fast=head.next; //快指针
        while(fast!=null&&fast.next!=null){
            //有环
            if(slow==fast){
                return true;
            }
            fast=fast.next.next;// 快指针步长为2
            slow=slow.next;//慢指针步长为1
        }
        return false;
    }
    public static void main(String[] args) {
        Node node1=new Node(1,"小明");
        Node node2=new Node(2,"小红");
        Node node3=new Node(3,"小李");
        Node node4=new Node(4,"张三");
        Node node5=new Node(5,"李四");
        Node node6=new Node(6,"王五");
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        node5.next=node6;
        node6.next=node1;
        System.out.println(isRing(node1));
    }
}
